unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ComCtrls,
  Menus, CreateNew, Grids, Buttons, DOM, XMLWrite, XMLRead, StdCtrls;

type

  { TfrmMain }
  TAccounts = class
            nBalance : Single;
  end;


  TfrmMain = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    lvwAccount: TListView;
    MenuItem1: TMenuItem;
    mnuCreateNew: TMenuItem;
    mnuAbout: TMenuItem;
    mnuHelp: TMenuItem;
    mnuExit: TMenuItem;
    mnuFile: TMenuItem;
    mnuMain: TMainMenu;
    pgMain: TPageControl;
    tabBalance: TTabSheet;
    tabAccounts: TTabSheet;
    tabReports: TTabSheet;
    tabTransaction: TTabSheet;
    tvwAccount: TTreeView;
    procedure Button1Click(Sender: TObject);
    procedure mnuCreateNewClick(Sender: TObject);
    procedure mnuExitClick(Sender: TObject);
    procedure tvwAccountChange(Sender: TObject; Node: TTreeNode);
    procedure UpdateListView;
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  frmMain: TfrmMain;
  


implementation

{ TfrmMain }

procedure TfrmMain.mnuExitClick(Sender: TObject);
begin
     Application.Terminate ;
end;

procedure TfrmMain.tvwAccountChange(Sender: TObject; Node: TTreeNode);
begin
     UpdateListView ;
end;

procedure TfrmMain.mnuCreateNewClick(Sender: TObject);
begin
     frmCreateNew.ShowModal ;

end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
   XMLDoc : TXMLDocument;
   iNode  : TDOMNode;
    procedure ProcessNode(
          Node : TDOMNode;
          tn   : TTreeNode);
    var
      cNode : TDOMNode;
      e     : TDOMElement;
      n     : string;
    begin
      if Node = nil then Exit;
      cNode := Node;
      if Node.NodeName='Account' then
      begin
           e := Node as TDOMElement;
           tn := tvwAccount.Items.AddChild(tn,e.GetAttribute('name'));
           n := e.GetAttribute('initial');
           if ((n <> '0.00') and (n<>'')) then
           begin
                tn.data := TAccounts.Create ;
                TAccounts(tn.Data).nBalance := single(StrToFloat(n));
           end;
      end
      else
      begin
           tn    := tvwAccount.Items.AddChild(tn,Node.NodeName);
      end;
{      end
      else
      begin
           cNode := Node;
           tn    := tvwAccount.Items.Add(tn,Node.NodeName);
      end;
}      cNode := Node.FirstChild;
      while cNode <> nil do
      begin
        ProcessNode(cNode, tn);
        cNode := cNode.NextSibling;
      end;
    end; (*ProcessNode*)

begin
     tvwAccount.Items.Clear;
     ReadXMLFile(XMLDoc,'accounts.sal');
     iNode := XMLDoc.DocumentElement.FindNode('Accounts');
     iNode := iNode.FirstChild;
     while iNode<>nil do
     begin
          ProcessNode(iNode,nil);
          iNode := iNode.NextSibling;
     end;
     XMLDoc.Destroy;
end;

procedure tfrmMain.UpdateListView;
var
   ChildNode: TTreeNode;
   sText    : String;
begin
     lvwAccount.Clear ;

     // make sure something is selected
     if (tvwAccount.Selected=nil) then
        Exit;

     // the common 'one folder above' link
     if (tvwAccount.Selected.Parent <> nil) then
     begin
          with lvwAccount.items.add do
          begin
               Caption := 'To Parent(todo)';
               Data    := tvwAccount.Selected.Parent.Data ;
          end;
     end;

     // supposed to speed up processing - complemented by an endupdate
     tvwAccount.Items.BeginUpdate ;

     ChildNode := tvwAccount.Selected.GetFirstChild ;
     while (ChildNode <> nil) do
     begin
          with lvwAccount.Items.Add do
          begin
               Caption := ChildNode.Text;
               Data    := ChildNode.Data;

               // for default items, set balance=0;
               if (TAccounts(childnode.Data) = nil) then
               begin
                  ChildNode.Data := TAccounts.Create;
                  TAccounts(ChildNode.data).nBalance := 0.00;
               end;


       	       str(TAccounts(ChildNode.Data).nBalance:10:2,sText);

               // TODO: dont display for income and expense accounts
               // dont display amount is it has further children
               if (ChildNode.HasChildren) then
                  sText := 'Have subaccounts';
               SubItems.Add(sText);
               //sText := ChildNode.GetParentNodeOfAbsoluteLevel(0).Text ;
               //SubItems.Add(sText);
          end;
          ChildNode := tvwAccount.Selected.GetNextChild(ChildNode);
     end;
     tvwAccount.Items.EndUpdate;

end;


initialization
  {$I main.lrs}

end.

